/*
 * Copyright 2006 Matt Jensen
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */
package org.spectaql.internal.metadata;

import org.spectaql.metadata.PersistenceMetadata;
import org.spectaql.metadata.EntityMetadata;

import java.util.HashSet;
import java.util.Set;

/**
 * <p>
 * <code>PersistenceMetadataBuilder</code> is a builder object which is used to construct {@link PersistenceMetadata}
 * instances.
 * </p>
 */
public class PersistenceMetadataBuilder
{
    /**
     * <p>
     * Entity metadata . 
     * </p>
     */
    private final Set<EntityMetadata> m_entityMetadata = new HashSet<EntityMetadata>();

    /**
     * <p>
     * Construct a {@link PersistenceMetadataBuilder} instance.
     * </p>
     */
    public PersistenceMetadataBuilder()
    {
        super();
    }

    /**
     * <p>
     * Add metadata for an entity to the builder.
     * </p>
     *
     * @param entityMetadata the entity metadata.
     * @return {@link PersistenceMetadataBuilder} this instance.
     */
    public PersistenceMetadataBuilder addEntity(final EntityMetadata entityMetadata)
    {
        m_entityMetadata.add(entityMetadata);
        return this;
    }

    /**
     * <p>
     * Create an {@link EntityMetadataBuilder} through which information on an entity can be added to this builder.
     * </p>
     *
     * @param entityName the entity name.
     * @param entityClass the entity class.
     * @param identifierClass the identifier class.
     * @return {@link EntityMetadataBuilder} instance.
     */
    public final EntityMetadataBuilder buildEntity(final String entityName,
                                                   final Class<?> entityClass,
                                                   final Class<?> identifierClass)
    {
        return createEntityMetadataBuilder(entityName, entityClass, identifierClass);
    }

    /**
     * <p>
     * Build a {@link PersistenceMetadata} instance from the information which has been stored in this builder instance.
     * </p>
     *
     * @return {@link PersistenceMetadata} instance.
     */
    public final PersistenceMetadata toPersistenceMetadata()
    {
        return createPersistenceMetadata(m_entityMetadata);
    }

    /**
     * <p>
     * Create a new {@link EntityMetadataBuilder} instance suitable for building an {@link EntityMetadata} instance for
     * a given entity class.
     * </p>
     *
     * @param entityName the entity name.
     * @param entityClass the entity class.
     * @param identifierClass the identifier class.
     * @return {@link EntityMetadataBuilder} instance.
     */
    protected EntityMetadataBuilder createEntityMetadataBuilder(final String entityName,
                                                                final Class<?> entityClass,
                                                                final Class<?> identifierClass)
    {
        return new EntityMetadataBuilder(entityName, entityClass, identifierClass);
    }

    /**
     * <p>
     * Create a {@link PersistenceMetadata} instance from given persistence information.
     * </p>
     * 
     * @param entityMetadata the entity metadata objects.
     * @return {@link PersistenceMetadata} instance.
     */
    protected PersistenceMetadata createPersistenceMetadata(final Set<EntityMetadata> entityMetadata)
    {
        return new PersistenceMetadataImpl(entityMetadata);
    }
}
